{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import os\n",
    "import matplotlib.font_manager as fm\n",
    "import scipy.stats as stats\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import scipy\n",
    "from docx import Document\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#读取预处理过的数据\n",
    "original_Catagory_Groupbydate = pd.read_excel('./results/销售量按分类-日统计.xlsx')\n",
    "original_Catagory = pd.read_excel('./results/销售量按分类-月统计.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "水生根茎类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -2.962\n",
      "P-value                         0.039\n",
      "Lags                                0\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.63 (1%), -2.95 (5%), -2.61 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "花叶类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -2.455\n",
      "P-value                         0.127\n",
      "Lags                                0\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.63 (1%), -2.95 (5%), -2.61 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "花菜类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -2.828\n",
      "P-value                         0.054\n",
      "Lags                                0\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.63 (1%), -2.95 (5%), -2.61 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "茄类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.619\n",
      "P-value                         0.005\n",
      "Lags                                0\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.63 (1%), -2.95 (5%), -2.61 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "辣椒类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.028\n",
      "P-value                         0.032\n",
      "Lags                                0\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.63 (1%), -2.95 (5%), -2.61 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "食用菌数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.136\n",
      "P-value                         0.024\n",
      "Lags                                3\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.65 (1%), -2.96 (5%), -2.62 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n"
     ]
    }
   ],
   "source": [
    "#进行品类的ADF检验\n",
    "from arch.unitroot import ADF\n",
    "column_data  = original_Catagory[['水生根茎类', '花叶类', '花菜类', '茄类', '辣椒类', '食用菌']]\n",
    "for column in column_data.columns:\n",
    "    result = ADF(column_data[column])\n",
    "    adf = ADF(column_data[column], trend='ct')\n",
    "\n",
    "    \n",
    "    # 打印出检验结果\n",
    "    print(f\"\\n\\n{column}数据的检验\\n\\n\")\n",
    "    print(\"不指定trend默认为检验是否含截距项平稳\\n\")\n",
    "    print(result.summary().as_text())\n",
    "\n",
    "    # print(\"\\n\\n指定trend为检验是否含截距项和时间趋势项平稳\\n\")\n",
    "    # print(adf.summary().as_text())\n",
    "\n",
    "\n",
    "# 说明：\n",
    "# arch包中ADF检验可指定trend为\n",
    "# 'n'（不含截距项和时间趋势项）\n",
    "# 'c'（含截距项）\n",
    "# 'ct'（含截距项和时间趋势项）\n",
    "# 'ctt'（含截距项和时间趋势项和二次型时间趋势项）\n",
    "# 分别对应不同平稳类型的检验。（滞后期lags默认为AIC最小）\n",
    "\n",
    "# 第一个输出表示未指定trend参数进行ADF检验，默认检验是否含有截距项平稳。\n",
    "# 在这种情况下，显著性水平为p大于0.05的阈值，是不能拒绝原假设的，即数据在不含截距项的情况下是非平稳的。\n",
    "\n",
    "# 第二个输出表示指定了trend参数为检验是否含截距项和时间趋势项平稳。\n",
    "# 在这种情况下，显著性水平为p小于0.05的阈值，是可以拒绝原假设，即数据在含有截距项和时间趋势项的情况下是趋势项平稳的。\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "水生根茎类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.102\n",
      "P-value                         0.026\n",
      "Lags                               19\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.44 (1%), -2.86 (5%), -2.57 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "花叶类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -2.819\n",
      "P-value                         0.056\n",
      "Lags                               20\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.44 (1%), -2.86 (5%), -2.57 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "花菜类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.129\n",
      "P-value                         0.024\n",
      "Lags                               20\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.44 (1%), -2.86 (5%), -2.57 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "茄类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.693\n",
      "P-value                         0.004\n",
      "Lags                               20\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.44 (1%), -2.86 (5%), -2.57 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "辣椒类数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.631\n",
      "P-value                         0.005\n",
      "Lags                               13\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.44 (1%), -2.86 (5%), -2.57 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n",
      "\n",
      "\n",
      "食用菌数据的检验\n",
      "\n",
      "\n",
      "不指定trend默认为检验是否含截距项平稳\n",
      "\n",
      "   Augmented Dickey-Fuller Results   \n",
      "=====================================\n",
      "Test Statistic                 -3.558\n",
      "P-value                         0.007\n",
      "Lags                               15\n",
      "-------------------------------------\n",
      "\n",
      "Trend: Constant\n",
      "Critical Values: -3.44 (1%), -2.86 (5%), -2.57 (10%)\n",
      "Null Hypothesis: The process contains a unit root.\n",
      "Alternative Hypothesis: The process is weakly stationary.\n"
     ]
    }
   ],
   "source": [
    "#进行单品的ADF检验\n",
    "column_data  = original_Catagory_Groupbydate[['水生根茎类', '花叶类', '花菜类', '茄类', '辣椒类', '食用菌']]\n",
    "for column in column_data.columns:\n",
    "    result = ADF(column_data[column])\n",
    "    adf = ADF(column_data[column], trend='ct')\n",
    "\n",
    "        # 打印出检验结果\n",
    "    print(f\"\\n\\n{column}数据的检验\\n\\n\")\n",
    "    print(\"不指定trend默认为检验是否含截距项平稳\\n\")\n",
    "    print(result.summary().as_text())\n",
    "\n",
    "    # print(\"\\n\\n指定trend为检验是否含截距项和时间趋势项平稳\\n\")\n",
    "    # print(adf.summary().as_text())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
